package com.dengqi.mymodule.tools;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Tools {

    /**
     * 获取当前程序的版本名
     */
    public static String getVersionName(Context mContext) {
        //获取packagemanager的实例
        PackageManager packageManager = mContext.getApplicationContext().getPackageManager();
        //getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(mContext.getApplicationContext().getPackageName(), 0);
            return packInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 获取当前程序的版本号
     */
    public static int getVersionCode(Context mContext) {
        //获取packagemanager的实例
        PackageManager packageManager = mContext.getApplicationContext().getPackageManager();
        //getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = null;
        try {
            packInfo = packageManager.getPackageInfo(mContext.getApplicationContext().getPackageName(), 0);
            return packInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return 1;
    }
}
